package org.flower.isme.flower.controller;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.flower.isme.common.preview.Preview;
import org.flower.isme.common.response.Page;
import org.flower.isme.common.response.R;
import org.flower.isme.flower.domain.dto.*;
import org.flower.isme.flower.domain.entity.Plants;
import org.flower.isme.flower.domain.request.*;
import org.flower.isme.flower.service.PlantEnvironmentService;
import org.flower.isme.flower.service.PlantsService;
import org.springframework.web.bind.annotation.*;

/**
 * 植物管理
 */
@RestController
@RequestMapping("/plants")
@RequiredArgsConstructor
@Tag(name = "植物")
@Slf4j
public class PlantsController {
    private final PlantsService plantsService;
    private final PlantEnvironmentService plantEnvironmentService;

    /**
     * 查询植物列表
     *
     * @param plantsPageRequest 查询条件
     * @return R
     */
    @GetMapping("/list")
    @Preview
    @Operation(summary = "查询植物列表")
    public R<Page<PlantsPageDto>> getAllPlants(PlantsPageRequest plantsPageRequest) {
        return R.ok(plantsService.queryPage(plantsPageRequest));
    }

    /**
     * 添加植物
     *
     * @param plants 植物
     * @return R
     */
    @PostMapping("/insert")
    @Preview
    @Operation(summary = "添加植物")
    public R<Void> insertPlant(@RequestBody @Valid CreatePlants plants) {
        Plants save = new Plants();
        save.setPlantName(plants.getPlantName());
        save.setLocation(plants.getLocation());
        save.setNickName(plants.getNickName());
        save.setLabelName(plants.getLabelName());
        plantsService.save(save);
        return R.ok();
    }

    /**
     * 修改植物
     *
     * @param update 修改信息
     * @return R
     */
    @PostMapping("/update")
    @Preview
    @Operation(summary = "修改植物")
    public R<Void> updatePlant(@RequestBody @Valid UpdatePlantRequest update) {
        LambdaUpdateWrapper<Plants> plantsLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        plantsLambdaUpdateWrapper.eq(ObjectUtil.isNotNull(update.getId()), Plants::getId, update.getId())
                .set(Plants::getPlantName, update.getPlantName())
                .set(Plants::getLocation, update.getLocation())
                .set(Plants::getLabelName, update.getLabelName())
                .set(Plants::getNickName, update.getNickName());
        plantsService.update(plantsLambdaUpdateWrapper);
        return R.ok();
    }

    /**
     * 通过id删除植物m
     *
     * @param id 植物id
     * @return R
     */
    @DeleteMapping("/remove/{id}")
    @Preview
    @Operation(summary = "通过id删除植物")
    public R<Void> removePlant(@PathVariable Long id) {
        plantsService.removePlant(id);
        return R.ok();
    }

    /**
     * 通过id上传图片路径
     *
     * @param update 信息
     * @return R
     */
    @PostMapping("/upload/image")
    @Preview
    @Operation(summary = "通过id上传图片路径")
    public R<String> uploadImage(@RequestBody UpdatePlantRequest update) {
        log.info("上传信息: {}", update);
        plantsService.uploadImage(update);
        return R.ok(null, "上传成功");
    }

    /**
     * 通过id获取植物详情
     *
     * @param id 植物id
     * @return R
     */
    @GetMapping("/details/{id}")
    @Preview
    @Operation(summary = "通过id获取植物详情")
    public R<PlantsDetailsDto> queryPlantsDetailsByPlantId(@PathVariable Long id) {
        return R.ok(plantsService.queryPlantsDetails(id));
    }

    @GetMapping("/flower/{id}")
    @Operation(summary = "通过id获取鲜花详情")
    public R<FlowerDetailsDto> queryFlowerDetailsById(@PathVariable Long id) {
        return R.ok(plantsService.queryFlowerDetailsByPlantId(id));
    }

    /**
     * 根据id更新植物生长环境
     *
     * @param request 生长环境请求
     * @return R
     */
    @PostMapping("/update/environment")
    @Preview
    @Operation(summary = "根据id更新植物生长环境")
    public R<Void> updatePlantsEnvironment(@RequestBody PlantEnvironmentRequest request) {
        plantEnvironmentService.savePlantEnvironment(request);
        return R.ok();
    }

    /**
     * 根据id查询植物生长环境
     *
     * @param plantId 植物id
     * @return R
     */
    @GetMapping("/environment/{plantId}")
    @Preview
    @Operation(summary = "根据id查询植物生长环境")
    public R<PlantEnvironmentDto> selectPlantEnvironment(@PathVariable Long plantId) {
        return R.ok(plantEnvironmentService.getPlantEnvironmentDtoById(plantId));
    }

    /**
     * 根据id更新植物描述
     *
     * @param update 描述
     * @return R
     */
    @PostMapping("/update/desc")
    @Preview
    @Operation(summary = "根据id更新植物描述")
    public R<Void> updatePlantDescription(@RequestBody UpdatePlantRequest update) {
        plantsService.updatePlantsDescription(update);
        return R.ok();
    }

    /**
     * 获取环境数据
     *
     * @return R
     */
    @GetMapping("/envirment/data")
    @Operation(summary = "获取环境数据")
    public R<FlowerEnvirmentDataDto> queryFlowerEnvirmentData() {
        return R.ok(plantsService.queryFlowerEnvirmentData());
    }

    /**
     * 根据id更新鲜花详情(APP)
     *
     * @param request 详情
     * @return R
     */
    @PostMapping("/update/flower/envir")
    @Operation(summary = "根据id更新鲜花详情(APP)")
    public R<Void> updateFlowerEnvirmentByPlantId(@RequestBody PlantEnvironmentRequest request) {
        plantEnvironmentService.updatePlantEnvironmentByPlantId(request);
        return R.ok();
    }
}
